package controllers

import (
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
)

type TestController struct {
	beego.Controller
}

//Res 这里犯错了，字段必须大写开头，不然返回的是空json。使用tag修改json显示
//You need to export the fields in ErrorJson by starting the name with an uppercase character.
//Use field tags to specify the lowercase names in the output.

type Res struct {
	Code int8                   `json:"code,omitempty"`
	Data map[string]interface{} `json:"data,omitempty"`
	Msg  string                 `json:"msg,omitempty"`
}

func (c *TestController) Get() {
	name := c.GetString("name", "guest")
	age, err := c.GetInt("age")
	if err != nil {
		fmt.Println("获取age出错", err)
	}
	var val map[string]interface{}
	val = make(map[string]interface{}, 3)
	val["name"] = name
	val["age"] = age

	res := Res{
		Code: 1,
		Data: val,
		Msg:  "success",
	}

	//[可选] 使用map定义一个响应body
	//var res map[string]interface{}
	//res=make(map[string]interface{},3)
	//res["code"]=0
	//res["msg"]="success"
	//res["data"]=val

	fmt.Printf("%v", res)
	c.Data["json"] = res
	_ = c.ServeJSON()
}
